<?php
namespace Common\Model;
use Think\Model;
class CarsModel extends Model{

	public function car_list($lastindex,$maxnum,$citys,$ordertype,$keybr,$keyse,$keypr,$keymo,$keyta,$keyag,$keylo,$keydi,$keyno,$keyco,$keyen,$keysea,$keyoi,$keycon,$keywat,$keyke,$cityormakert)
	{
		if(!is_numeric( $lastindex ) || !is_numeric( $maxnum ) || $maxnum > 40)
		return array("Ret" => 1,"Data" =>"","Msg" => "当前页码错误$lastindex $maxnum错误");
		$orderby=  ' Order by CheckDate DESC,ViewNum desc';
		switch($ordertype)
		{
			case "pa":
				$orderby = ' Order by CarPrice ASC';
				break;
			case "pd":
				$orderby = ' Order by CarPrice DESC';
				break;
			case "n":
				$orderby = ' Order by CheckDate DESC';
				break;
			case "a":
				$orderby = ' Order by carlicdate DESC';
				break;
			case "l":
				$orderby = ' Order by carrunlen ASC';
				break;
			case "ka"://根据品牌升序
				$orderby = ' Order by CarK ASC';
			case "kd"://根据品牌降序
				$orderby = ' Order by CarK DESC';
		}
		
		/*城市/市场过滤条件*/
		if(empty($citys))
		return array("Ret" => 1,"Data" =>"","Msg" => "城市或市场不能为空");
		if(inject_check($citys))
		return array("Ret" => 1,"Data" =>"","Msg" => "城市或市场存在敏感搜索词");
		$citys = str_replace(",","','",$citys);
		$citys = "'$citys'";
		
		$whereands = "";
		
		/*搜索品牌条件*/
		if(!empty($keybr))
		{
			if(inject_check($keybr))
			return array("Ret" => 1,"Data" =>"","Msg" => "品牌存在敏感搜索词");
			$whereands .= " and CarBrand = '$keybr'";
		}
		
		/*搜索车系条件*/
		if(!empty($keyse))
		{
			if(inject_check($keyse))
			return array("Ret" => 1,"Data" =>"","Msg" => "车系存在敏感搜索词");
			$whereands .= " and CarSeries = '$keyse'";
		}
		
		/*搜索报价条件*/
		if(!empty($keypr))
		{
			if(inject_check($keypr))
			return array("Ret" => 1,"Data" =>"","Msg" => "报价存在敏感搜索词");
			if(strpos($keypr, '-') !== false)
			{
				$minmax = explode ('-',$keypr);
				
				if(count($minmax) != 2 || !is_numeric( $minmax[0] ) || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keypr错误");
				$whereands .= " and ( CarPrice between $minmax[0] and $minmax[1] ) ";
			}
			elseif(strpos($keypr, '>') !== false)
			{
				$minmax = explode ('>',$keypr);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keypr错误");
				$whereands .= " and (CarPrice > $minmax[1])";
			}
			elseif(strpos($keypr, '<') !== false)
			{
				$minmax = explode ('<',$keypr);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keypr错误");
				$whereands .= " and (CarPrice < $minmax[1])";
			}
		}
		
		/*搜索箱型条件*/
		if(!empty($keymo))
		{
			if(inject_check($keymo))
			return array("Ret" => 1,"Data" =>"","Msg" => "箱型存在敏感搜索词");
			if($keymo=="轿车")
			$whereands .= " and (CarBox in ('三厢轿车','两厢轿车'))";
			else
			$whereands .= " and CarBox = '$keymo'";
		}
		
		/*搜索标签条件*/
		if(!empty($keyta))
		{
			if(inject_check($keyta))
			return array("Ret" => 1,"Data" =>"","Msg" => "标签存在敏感搜索词");
			if(strpos($keyta, '练手车') !== false)
			$whereands .= " and CarPrice < 5.01 ";
			if(strpos($keyta, '准新车') !== false)
			$whereands .= " and ( DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS( CarLicDate)),'%Y') + 0 < 1.1 ) ";
			if(strpos($keyta, '寄售') !== false)
			$whereands .= " and CarFullSale = 1 ";
			if(strpos($keyta, '急售') !== false)
			$whereands .= " and CarWantSale = 1 ";
			if(strpos($keyta, '今日超值') !== false)
			$whereands .= " and TO_DAYS(CheckDate)=TO_DAYS(now()) and CarPrice < 12.01 ";
		}	
		
		/*搜索车龄条件*/
		if(!empty($keyag))
		{
			if(inject_check($keyag))
			return array("Ret" => 1,"Data" =>"","Msg" => "车龄存在敏感搜索词");
			if(strpos($keyag, '-') !== false)
			{
				$minmax = explode ('-',$keyag);
				if(count($minmax) != 2 || !is_numeric( $minmax[0] ) || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "车龄$keyag错误");
				$whereands .= " and ( DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS( CarLicDate)),'%Y') + 0 between $minmax[0] and $minmax[1] ) ";
			}
			elseif(strpos($keyag, '>') !== false)
			{
				$minmax = explode ('>',$keyag);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "车龄$keyag错误");
				$whereands .= " and (DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS( CarLicDate)),'%Y') + 0 > $minmax[1]) ";
			}
			elseif(strpos($keyag, '<') !== false)
			{
				$minmax = explode ('<',$keyag);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "车龄$keyag错误");
				$whereands .= " and (DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS( CarLicDate)),'%Y') + 0 < $minmax[1]) ";
			}
		}
		
		/*搜索里程条件*/
		if(!empty($keylo))
		{
			if(inject_check($keylo))
			return array("Ret" => 1,"Data" =>"","Msg" => "里程存在敏感搜索词");
			if(strpos($keylo, '-') !== false)
			{
				$minmax = explode ('-',$keylo);
				if(count($minmax) != 2 || !is_numeric( $minmax[0] ) || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keylo错误");
				$whereands .= " and ( CarRunLen  between $minmax[0] and $minmax[1] ) ";
			}
			elseif(strpos($keylo, '>') !== false)
			{
				$minmax = explode ('>',$keylo);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keylo错误");
				$whereands .= " and (CarRunLen  > $minmax[1]) ";
			}
			elseif(strpos($keylo, '<') !== false)
			{
				$minmax = explode ('<',$keylo);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keylo错误");
				$whereands .= " and (CarRunLen < $minmax[1]) ";
			}
		}
		
		/*搜索排量条件*/
		if(!empty($keydi))
		{
			if(inject_check($keydi))
			return array("Ret" => 1,"Data" =>"","Msg" => "排量存在敏感搜索词");
			if(strpos($keydi, '-') !== false)
			{
				$minmax = explode ('-',$keydi);
				if(count($minmax) != 2 || !is_numeric( $minmax[0] ) || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keydi错误");
				$whereands .= " and ( CarEmissions  between $minmax[0] and $minmax[1] ) ";
			}
			elseif(strpos($keydi, '>') !== false)
			{
				$minmax = explode ('>',$keydi);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keydi错误");
				$whereands .= " and (CarEmissions  > $minmax[1]) ";
			}
			elseif(strpos($keydi, '<') !== false)
			{
				$minmax = explode ('<',$keydi);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "售价$keydi错误");
				$whereands .= " and (CarEmissions < $minmax[1]) ";
			}
		}
		
		/*排放标准过滤条件*/
		if(!empty($keyno))
		{
			if(inject_check($keyno))
			return array("Ret" => 1,"Data" =>"","Msg" => "排放标准存在敏感搜索词");
			$keyno = str_replace(",","','",$keyno);
			$whereands .= "and (CarEmKind in ('$keyno')) ";
		}
		
		/*颜色过滤条件*/
		if(!empty($keyco))
		{
			if(inject_check($keyco))
			return array("Ret" => 1,"Data" =>"","Msg" => "颜色存在敏感搜索词");
			$keyco = str_replace(",","','",$keyco);
			$whereands .= "and (CarColor in ('$keyco')) ";
		}
		
		/*变速箱过滤条件*/
		if(!empty($keyen))
		{
			if(inject_check($keyen))
			return array("Ret" => 1,"Data" =>"","Msg" => "变速箱存在敏感搜索词");
			$whereands .= "and CarGearbox = '$keyen' ";
		}
		
		/*座位过滤条件*/
		if(!empty($keysea))
		{
			if(inject_check($keysea))
			return array("Ret" => 1,"Data" =>"","Msg" => "座位存在敏感搜索词");
			if(strpos($keysea, '-') !== false)
			{
				$minmax = explode ('-',$keysea);
				if(count($minmax) != 2 || !is_numeric( $minmax[0] ) || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "座位$keysea错误");
				$whereands .= " and ( CarSeatNum  between $minmax[0] and $minmax[1] ) ";
			}
			elseif(strpos($keysea, '>') !== false)
			{
				$minmax = explode ('>',$keysea);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "座位$keysea错误");
				$whereands .= " and (CarSeatNum  > $minmax[1]) ";
			}
			elseif(strpos($keysea, '<') !== false)
			{
				$minmax = explode ('<',$keysea);
				if(count($minmax) != 2 || !is_numeric($minmax[1]) )
				return array("Ret" => 1,"Data" =>"","Msg" => "座位$keysea错误");
				$whereands .= " and (CarSeatNum < $minmax[1]) ";
			}
		}
		/*燃油过滤条件*/
		if(!empty($keyoi))
		{
			if(inject_check($keyoi))
			return array("Ret" => 1,"Data" =>"","Msg" => "燃油存在敏感搜索词");
			$keyoi = str_replace(",","','",$keyoi);
			$whereands .= "and (CarOil in ('$keyoi')) ";
		}
		
		/*国别过滤条件*/
		if(!empty($keycon))
		{
			if(inject_check($keycon))
			return array("Ret" => 1,"Data" =>"","Msg" => "国别存在敏感搜索词");
			$keycon = str_replace(",","','",$keycon);
			$whereands .= "and (CarContry in ('$keycon')) ";
		}
		$whereands .= " and carstatus > 2 ";
		/*是否急售*/
		if($keywat == "1" )
		{
			$whereands .= "and (carwantsale = '1') ";
		}
		/*搜索关键词条件*/
		if(!empty($keyke))
		{
			$whereands .= " and instr(CONCAT_WS('',CarBrand,CarSeries,CarYear,CarEmissions,CarKInd,CarGearbox,CarColor,CarContry),'$keyke')> 0";
			//$whereands .= " and (CONCAT_WS('',CarBrand,CarSeries,CarYear,CarEmissions,CarKInd,CarGearbox,CarColor,CarContry) like '%$keyke%')";
		}
		/*选择的城市/市场*/
		if($cityormakert == "1")
		{
			$whereands  = " where  c.carc in ($citys) and c.CarStatus > 2 and c.CarStatus <16  " . $whereands;
			return $this->listcs($whereands,$lastindex ,$maxnum,$orderby);
		}
		else
		{
			$whereands  = " where mk.Mkname in ($citys) and c.CarStatus > 2 and c.CarStatus <16 " . $whereands;
			return $this->listmks($whereands,$lastindex ,$maxnum,$orderby);
		}
	}

	/*
		城市搜索，根据市场默认显示
		*/
	public function listmks($wheres,$lastindex,$maxnum,$orderby)
	{
		try
		{
			$allnum = 0;
			$Model = new \Think\Model();
			if($lastindex == 0)
			{
				$sql = "select count(*) as ct from nc_cars c inner join nc_market mk on mkid = mk.id $wheres;";
				$allnum = $Model->query($sql)[0]['ct'];
			}
			$lastindex = $lastindex * $maxnum;
			$sql = "select c.id,ifnull(GROUP_CONCAT(m.id ORDER BY orderno ),'') as imgs,trim(concat_ws(' ',if(locate(carbrand,carseries),'',carbrand),carseries,concat(caryear,'款'),caremissions,carkind)) as na,concat_ws('/',date_format(carlicdate,'%Y年'),concat_ws('',carrunlen,'万公里')) as ds,carprice as cp,ifnull(carwantsale,0)  as cw,ifnull(carfullsale,0) as cf,if(TO_DAYS(CheckDate)=TO_DAYS(now()),1,0) as ct,if((now() - carlicdate < 365),1,0) as cn,
				ifnull(c.CarNewPrice,0) as newprice from nc_cars c inner join nc_market mk on c.mkid = mk.id left join nc_carimage m on c.id=m.carid and m.orderno < 6 $wheres group by c.id  $orderby limit $lastindex,$maxnum;";
			
			$list = $Model->query($sql);
			return array("Ret" => 0,"Data" => $list,"Msg" => $allnum);
		}
		catch(Exception $e)
		{
			return array("Ret" => 1,"Data" =>"","Msg" => $e->getMessage());
		}
	}
	/*
		城市搜索，根据城市默认显示
		*/
	public function listcs($wheres,$lastindex,$maxnum,$orderby,$extend="")
	{
		try
		{
			$allnum = 0;
			$Model = new \Think\Model();
			if($lastindex == 0)
			{
				$sql = "select count(*) as ct from nc_cars c  $wheres;";
				$allnum = $Model->query($sql)[0]['ct'];
			}
			$lastindex = $lastindex * $maxnum;
			$sql = "select c.id,ifnull(GROUP_CONCAT(distinct m.id ORDER BY orderno ),'') as imgs,trim(concat_ws(' ',if(locate(carbrand,carseries),'',carbrand),carseries,concat(caryear,'款'),caremissions,carkind)) as na,concat_ws('/',date_format(carlicdate,'%Y年'),concat_ws('',carrunlen,'万公里')) as ds,carprice as cp,ifnull(carwantsale,0)  as cw,ifnull(carfullsale,0) as cf, if(TO_DAYS(CheckDate)=TO_DAYS(now()),1,0) as ct,
				if((now() - carlicdate < 365),1,0) as cn  
				,ifnull(c.CarNewPrice,0) as newprice $extend from nc_cars c left join nc_carimage m on c.id=m.carid and m.orderno < 6 
				$wheres group by c.id $orderby limit $lastindex,$maxnum;";
			$list = $Model->query($sql);
			return array("Ret" => 0,"Data" => $list,"Msg" => $allnum);
		}
		catch(Exception $e)
		{
			return array("Ret" => 1,"Data" =>"","Msg" => $e->getMessage());
		}
	}
}

?>